package me.chan.stream;

import java.util.Arrays;
import java.util.List;
import static java.util.stream.Collectors.toList;

public class FlatMapDemo {

	public static void main(String[] args) {
		/*
		String[] arrayofWords = {"hello", "javaInAction"};
		//Stream<String> arrayStream = Arrays.stream(arrayofWords);
		List<String> uniqueWord = Arrays.asList(arrayofWords).stream()
															.map(w -> w.split("")).flatMap(Arrays::stream)
															.distinct().sorted().collect(toList());
		System.out.println(uniqueWord);
		
		List<Integer> numbers = Arrays.asList(1,2,3,4,5);
		numbers.stream().map(x -> x*x).forEach(System.out::println);
		 */
		List<Integer> num1 = Arrays.asList(1,2,3);
		List<Integer> num2 = Arrays.asList(3,4);
		List<Integer[]> pairs = num1.stream().flatMap(i -> num2.stream()
													.filter(j -> (i+j) % 3 == 0 ).map(j -> new Integer[]{i,j})
													).collect(toList());
		printIntArrList(pairs);
		/*for(Integer[] intArr : pairs) {
			for(Integer i : intArr) {
				System.out.println(i);
			}
		}*/
	}

	public static void printIntArrList(List<Integer[]> arrList) {
		StringBuilder sb = new StringBuilder("[ ");
		for(Integer[] intArr : arrList) {
			sb.append("(");
			for (Integer i : intArr) {
				sb.append(i).append(" ");
			}
			sb.append(")").append(" ");
		}
		sb.append("]");
		System.out.println(sb.toString());
	}
}
